/*
 * @Description:单表格增、删、改操作
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2022-08-01 10:43:06
 * @LastEditors: swc
 * @LastEditTime: 2024-02-02 17:34:24
 */
/* eslint-disable max-classes-per-file */

import lexicon from '@/lib/lexicon';
import { T_return } from '../base/return';
import { I_baseData, T_baseData } from './base/baseGridData';
import { I_fv } from './base/fv';
import { I_rule, T_rule } from './base/rule';
import { T_stdReq } from './base/stdReq';
import { I_funInfo, I_userInfo } from './event';

export class T_syncParams {
  protected apiName: string;

  protected userInfo: I_userInfo;

  protected funInfo: I_funInfo;

  protected syncData: any;

  public setApiName(p: string): T_syncParams {
    this.apiName = p;
    return this;
  }

  public setUserInfo(p: I_userInfo): T_syncParams {
    this.userInfo = p;
    return this;
  }

  public setFunInfo(p: I_funInfo): T_syncParams {
    this.funInfo = p;
    return this;
  }

  public setSyncData(p: any): T_syncParams {
    this.syncData = p;
    return this;
  }

  public getApiName(): string {
    return this.apiName;
  }

  public getUserInfo(): I_userInfo {
    return this.userInfo;
  }

  public getFunInfo(): I_funInfo {
    return this.funInfo;
  }

  public getSyncData(): any {
    return this.syncData;
  }
}
export interface I_syncParams extends T_syncParams {}

export class T_syncExec {
  protected befEvent: I_syncParams[];

  protected aftEvent: I_syncParams[];

  constructor() {
    this.befEvent = [];
    this.aftEvent = [];
  }

  public setBefEvent(...p: I_syncParams[]): T_syncExec {
    this.befEvent.push(...p);
    return this;
  }

  public setAftEvent(...p: I_syncParams[]): T_syncExec {
    this.aftEvent.push(...p);
    return this;
  }

  public getBefEvent(): Array<T_syncParams> {
    return this.befEvent;
  }

  public getAftEvent(): Array<T_syncParams> {
    return this.aftEvent;
  }
}

// 更新数据类
export class T_ctdUpdData extends T_baseData {
  // private url: string = lexicon.url.ctd;

  // mongo条件表达式
  filter: object;

  constructor() {
    super();
    this.filter = {};
  }

  // 通过Tfilter.parsexxx返回的结果
  public setFilter(bson: object): T_ctdUpdData {
    this.filter = bson;
    return this;
  }
}
export interface I_ctdUpdData extends T_ctdUpdData {}

export class T_ctdInsRec extends T_syncExec {
  // private url: string = lexicon.url.ctd;

  // mongo条件表达式
  scope: object;

  data: I_baseData[];

  constructor() {
    super();
    this.data = [] as I_baseData[];
  }

  public setScope(p: object): T_ctdInsRec {
    this.scope = p;
    return this;
  }

  /**
   * @description: 将基础数据插入到T_ctdInsRec数组
   * @param {I_baseData} p T_baseData
   * @return {*} 返回T_ctdInsRec实例
   * @author: wiz
   * @Date: 2022-12-01 12:18:01
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setBaseDataBulk(...p: I_baseData[]): T_ctdInsRec {
    this.data.push(...p);
    return this;
  }

  /**
   * @description: 将T_baseData数据明细增加到T_ctdInsRec.data
   * @param {object} rule T_baseData.rule
   * @param {object} fileinfo T_baseData.fileInfo
   * @param {array} fv 装饰fvs增加到T_baseData.rowData
   * @return {*}
   * @author: wiz
   * @Date: 2022-12-01 12:21:01
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setBaseDataGeneral(
    rule: I_rule,
    fileinfo: object,
    ...fv: I_fv[]
  ): T_ctdInsRec {
    const bData = new T_baseData();
    bData
      .setDataRule(rule)
      .setRowData(...fv)
      .setFileInfo(fileinfo);
    this.data.push(bData);
    return this;
  }
}
export interface I_ctdInsRec extends T_ctdInsRec {}

export class T_ctdUpdRec extends T_syncExec {
  // private url: string = lexicon.url.ctd;

  data: I_ctdUpdData[];

  constructor() {
    super();
    this.data = [] as I_ctdUpdData[];
  }

  /**
   * @description:将T_ctdUpdData数据明细增加到T_ctdUpdRec.data
   * @return {*} 返回T_ctdUpdRec类实例
   * @author: wiz
   * @Date: 2022-12-01 12:22:49
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setData(
    fileInfo: object,
    filter: object,
    rule: I_rule,
    ...fv: I_fv[]
  ): T_ctdUpdRec {
    const bData = new T_ctdUpdData();
    bData.setDataRule(rule).setFileInfo(fileInfo);
    bData.setFilter(filter).setRowData(...fv);
    this.data.push(bData);
    return this;
  }

  /**
   * @description: 新增一个T_ctdUpdData到T_ctdUpdRec.data
   * @param {I_ctdUpdData} p
   * @return {*} 返回T_ctdUpdRec类实例
   * @author: wiz
   * @Date: 2022-12-01 12:27:24
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setCtdUpdData(p: I_ctdUpdData): T_ctdUpdRec {
    this.data.push(p);
    return this;
  }

  public setCtdUpdDatas(...p: I_ctdUpdData[]): T_ctdUpdRec {
    this.data.push(...p);
    return this;
  }
}
export interface I_ctdUpdRec extends T_ctdUpdRec {}

export class T_ctdDelRec extends T_syncExec {
  // private url: string = lexicon.url.ctd;

  rule: I_rule;

  filter: object;

  public setRule(p: I_rule): T_ctdDelRec {
    this.rule = p;
    return this;
  }

  public setFilter(p: object): T_ctdDelRec {
    this.filter = p;
    return this;
  }
}
export interface I_ctdDelRec extends T_ctdDelRec {}

// ctd API 参数接口类
export class T_ctdArg extends T_stdReq {
  private url: string = lexicon.url.ctd;

  insertRecords?: I_ctdInsRec;

  removeRecords?: I_ctdDelRec;

  updateRecords?: I_ctdUpdRec;

  constructor() {
    super();
    this.rule = new T_rule();
    this.insertRecords = {} as T_ctdInsRec;
    this.removeRecords = {} as T_ctdDelRec;
    this.updateRecords = {} as T_ctdUpdRec;
  }

  public setTableName(p: string): T_ctdArg {
    this.tableName = p;
    return this;
  }

  public setDbType(p: string): T_ctdArg {
    this.dbType = p;
    return this;
  }

  public setRule(p: I_rule): T_ctdArg {
    this.rule = p;
    return this;
  }

  public setInsRec(p: I_ctdInsRec): T_ctdArg {
    this.insertRecords = p;
    return this;
  }

  public setDelRec(p: I_ctdDelRec): T_ctdArg {
    this.removeRecords = p;
    return this;
  }

  public setUpdRec(p: I_ctdUpdRec): T_ctdArg {
    this.updateRecords = p;
    return this;
  }

  /**
   * @description: 只有一行插入
   * @return {*}返回本类实例
   * @author: wiz
   * @Date: 2022-12-01 12:33:17
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */

  public setInsRecDataOne(
    scope: object,
    rule: I_rule,
    fileInfo: object,
    ...fv: I_fv[]
  ): T_ctdArg {
    const insRec = new T_ctdInsRec();
    insRec.setScope(scope).setBaseDataGeneral(rule, fileInfo, ...fv);
    this.insertRecords = insRec;
    return this;
  }

  /**
   * @description: 插入多行
   * @param {object} scope
   * @param {array} baseData
   * @return {*}
   * @author: wiz
   * @Date: 2022-12-04 15:52:41
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setInsRecDataMany(scope: object, ...baseData: I_baseData[]): T_ctdArg {
    const insRec = new T_ctdInsRec();
    insRec.setScope(scope).setBaseDataBulk(...baseData);
    this.insertRecords = insRec;
    return this;
  }

  public setInsRecSync(
    befEvent: I_syncParams[],
    aftEvent: I_syncParams[]
  ): T_ctdArg {
    this.insertRecords.setBefEvent(...befEvent);
    this.insertRecords.setAftEvent(...aftEvent);
    return this;
  }

  public setInsRecBefSync(befEvent: I_syncParams[]): T_ctdArg {
    this.insertRecords.setBefEvent(...befEvent);
    return this;
  }

  public setInsRecAftSync(aftEvent: I_syncParams[]): T_ctdArg {
    this.insertRecords.setAftEvent(...aftEvent);
    return this;
  }

  // 一次性设置del数据
  public setDelRecData(rule: I_rule, filter: object): T_ctdArg {
    const delRec = new T_ctdDelRec();
    delRec.setFilter(filter).setRule(rule);
    this.removeRecords = delRec;
    return this;
  }

  public setDelRecSync(
    befEvent: I_syncParams[],
    aftEvent: I_syncParams[]
  ): T_ctdArg {
    this.removeRecords.setBefEvent(...befEvent);
    this.removeRecords.setAftEvent(...aftEvent);
    return this;
  }

  public setDelRecBefSync(befEvent: I_syncParams[]): T_ctdArg {
    this.removeRecords.setBefEvent(...befEvent);
    return this;
  }

  public setDelRecAftSync(aftEvent: I_syncParams[]): T_ctdArg {
    this.removeRecords.setAftEvent(...aftEvent);
    return this;
  }

  /**
   * @description: 只有一行更新数据
   * @return {*} 本类实例
   * @author: wiz
   * @Date: 2022-12-04 15:51:02
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setUpdRecDataOne(
    fileInfo: object,
    filter: object,
    rule: I_rule,
    ...fv: I_fv[]
  ): T_ctdArg {
    const updRec = new T_ctdUpdRec();
    updRec.setData(fileInfo, filter, rule, ...fv);
    this.updateRecords = updRec;
    return this;
  }

  /**
   * @description: 一次更新多行数据
   * @param {array} ctdUpdData
   * @return {*}
   * @author: wiz
   * @Date: 2022-12-04 15:58:24
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */
  public setUpdRecDataMany(...ctdUpdData: I_ctdUpdData[]): T_ctdArg {
    const updRec = new T_ctdUpdRec();
    updRec.data.push(...ctdUpdData);
    this.updateRecords = updRec;
    return this;
  }

  public setUpdRecSync(
    befEvent: I_syncParams[],
    aftEvent: I_syncParams[]
  ): T_ctdArg {
    this.updateRecords.setBefEvent(...befEvent);
    this.updateRecords.setAftEvent(...aftEvent);
    return this;
  }

  public setUpdRecBefSync(befEvent: I_syncParams[]): T_ctdArg {
    this.updateRecords.setBefEvent(...befEvent);
    return this;
  }

  public setUpdRecAftSync(aftEvent: I_syncParams[]): T_ctdArg {
    this.updateRecords.setAftEvent(...aftEvent);
    return this;
  }

  /**
   * @description: 输出Json对象
   * @return {T_error} 如果不出错在T_error中通过getReturnData('ctdJson')得到数据
   * @author: wiz
   * @Date: 2022-12-01 14:24:28
   * @LastEditors: wiz
   * @LastEditTime: Do not edit
   */

  public outJson(): T_return {
    const err = new T_return();
    const obj: any = {
      tableName: this.tableName,
      dbType: this.dbType,
      rule: this.rule
    };
    let i = 0;
    if (Object.keys(this.insertRecords).length !== 0) {
      obj['insertRecords'] = this.insertRecords;
      i++;
    }
    if (Object.keys(this.updateRecords).length !== 0) {
      obj['updateRecords'] = this.updateRecords;
      i++;
    }
    if (Object.keys(this.removeRecords).length !== 0) {
      i++;
      obj['removeRecords'] = this.removeRecords;
    }
    if (i === 0) {
      err.errmsg = `-119,FE,80`; // 数据为空
      return err;
    }
    err.setReturnData('ctdJson', obj);
    return err;
  }
}

// ctd API参数接口
export interface I_ctdArg extends T_ctdArg {}
